import {Tween, Easing} from "solar/tween";

/** @noSelf **/
type FrameProp = {
    "visible": boolean,
    "position": { "x": number, "y": number },
    "size": { "width": number, "height": number },
    "ref": { "current": number }
};

export default class TweenUtil {


    static frameTransition(prop: FrameProp, duration: number = 200, x: number = 0, y: number = 0, w: number = 0, h: number = 0,
                           a: number = 150, delay: number = 0, rollback: boolean = false) {
        let srcObj = {x: prop.position.x, y: prop.position.y, w: prop.size.width, h: prop.size.height, a: 255}
        let toObj = {
            x: prop.position.x + x,
            y: prop.position.y + y,
            w: prop.size.width + w,
            h: prop.size.height + h,
            a: a
        }
        let tween1 = new Tween({...srcObj})
            .to(toObj, duration)
            .delay(delay)
            .easing(Easing.Quadratic.Out)
            .onUpdate((temp) => {
                DzFrameSetAlpha(prop.ref.current, temp.a)
                DzFrameSetPoint(prop.ref.current, FRAMEPOINT_BOTTOMLEFT, DzFrameGetParent(prop.ref.current), FRAMEPOINT_BOTTOMLEFT, temp.x, temp.y)
                DzFrameSetSize(prop.ref.current, temp.w, temp.h)
            });
        if (rollback) {
            let tween2 = new Tween(toObj)
                .to(srcObj, duration)
                .delay(delay)
                .easing(Easing.Quadratic.In)
                .onUpdate((temp) => {
                    DzFrameSetAlpha(prop.ref.current, temp.a)
                    DzFrameSetPoint(prop.ref.current, FRAMEPOINT_BOTTOMLEFT, DzFrameGetParent(prop.ref.current), FRAMEPOINT_BOTTOMLEFT, temp.x, temp.y)
                    DzFrameSetSize(prop.ref.current, temp.w, temp.h)
                });
            tween1.chain(tween2)
        }
        DzFrameClearAllPoints(prop.ref.current)
        tween1.start();
    }

}
